<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PeriodRecode;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class PeriodRecodeController extends AdminController
{

    public function index(Content $content)
    {
        return $content->title('课时记录')
            ->description('列表')
            ->body($this->grid());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PeriodRecode(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id','姓名')->display(function ($value){
                return  User::where('id',$value)->value('user_name') ?? '';
            });
            $grid->column('period');
            $grid->column('cause');
            $grid->column('created_at')->display(function ($value){
                return date('Y-m-d H:i:s',$value);
            });
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->where('user_id', function ($query) {
                    $query->whereHas('user', function ($query) {
                        $query->where('user_name', 'like', "%{$this->input}%");
                    });
                }, '姓名')->width(3);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->disableDelete();
                $actions->disableEdit();
//                $actions->disableQuickEidt();
                $actions->disableView();
            });
            //禁用新增按钮
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PeriodRecode(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('period');
            $show->field('cause');
            $show->field('created_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PeriodRecode(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('period');
            $form->text('cause');
            $form->text('created_at');
        });
    }
}
